/**
 * 将一张图片切割成为等角地图所需要的菱形tile
 * author:chris chen
 */

package com.youease.chris.utils
{
	import com.youease.chris.manager.GlobalObject;
	import com.youease.chris.manager.TexturesManager;
	import com.youease.chris.model.vo.TextureData;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.ByteArray;
	
	public class CutRhombus
	{
		private var operateData:BitmapData;
		private var sourceData:BitmapData;
		private var distData:BitmapData;
		private var row:int;
		private var col:int;
		private var distWidth:Number = GlobalObject.cellWidth;
		private var distHeight:Number = GlobalObject.cellHeight;
		private var pixelArr:Array = new Array();
		
		public function CutRhombus()
		{
			super();
		}
		
		public function cut(name:String,data:BitmapData):void {
			sourceData = data;
			clearWhite(sourceData);
			row = Math.ceil(data.height/distHeight)*2 + 1;
			col = Math.ceil(data.width/distWidth) + 1;
			
			var rowFlag:Boolean = data.width%distWidth>(distWidth/2);
			var colFlag:Boolean = data.height%distHeight>(distHeight/2);
			
//			if(rowFlag) col ++;
			
			
			var retreat:Boolean = false;
			var rx:Number = 0;
			var ry:Number = -(distHeight+distHeight/2);
			for(var r:int=0;r<row;r++) {
				for(var c:int=0;c<col;c++) {
					retreat = Boolean(r%2);
					if(retreat) {
						rx = -distWidth/2 + c*distWidth;
					}else {
						rx = c*distWidth;
					}
//					distData = new BitmapData(distWidth,distHeight*2,true,0x00000000);
					distData = new BitmapData(distWidth,distHeight*2);
					distData.copyPixels(sourceData,new Rectangle(rx,ry,distWidth,distHeight*2),new Point(0,0));
					
					var transparency:Boolean = true;
					
					for(var i:int=0;i<distData.width;i++) {
						for(var j:int=0;j<distData.height;j++) {
							var p:Point = new Point(i,j);
							if(inQuadrangle(new Point(0,distData.height*3/4),new Point(distData.width/2,distData.height/2),new Point(distData.width,distData.height*3/4),new Point(distData.width/2,distData.height),p)) {
								if(distData.getPixel32(i,j) == 0xffffffff) {
									distData.setPixel32(i,j,0x00000000);
								}else {
									transparency = false;
								}
							}else {
								distData.setPixel32(i,j,0x00000000);
							}
						}
					}
					if(!transparency) {
						var td:TextureData = new TextureData();
						td.type = -1;
						td.big = 1;
						td.img = name.substring(0,name.indexOf("."))+"/"+name.substring(0,name.indexOf("."))+"_"+r+"_"+c+".gif";
						td.bitmapdata = distData;
						TexturesManager.textures[td.img] = td;
						writeImage(td.img,PNGEncoder.encode(distData));
					}
					
				}
				ry += distHeight/2;
			}
		}
		
		/**
		 * 计算三角形面积 
		 */
		private function triangleArea(p1:Point,p2:Point,p3:Point):Number {
			var area:Number = Math.abs(
										(p1.x*p2.y + p2.x*p3.y + p3.x*p1.y 
										- p2.x*p1.y - p3.x*p2.y - p1.x*p3.y)/2.0
									   );
			return area;
		}
		/**
		 * 判断点p是否在a,b,c点构成的三角形内 
		 */
		private function inTriangle(pa:Point,pb:Point,pc:Point,p:Point):Boolean {
			var triangle:Number = triangleArea(pa,pb,pc);
			var area:Number = triangleArea(p,pa,pb) + triangleArea(p,pa,pc) + triangleArea(p,pb,pc);
			var epsilon:Number = 0.0001;
			if(Math.abs(triangle - area)<epsilon) {
				return true;
			}
			return false;
		}
		/**
		 * 判断点p是否在a,b,c,d点构成的四边形内 
		 */
		private function inQuadrangle(a:Point,b:Point,c:Point,d:Point,p:Point):Boolean {
			var quidArea:Number = triangleArea(a,b,c) + triangleArea(c,d,a);
			var triArea:Number = triangleArea(a,d,p) + triangleArea(b,c,p) + triangleArea(c,d,p) + triangleArea(b,a,p);
			var epsilon:Number = 0.0001;
			if(Math.abs(quidArea - triArea)<epsilon) {
				return true;
			}
			return false;
		}
		
		public function writeImage(name:String,bytes:ByteArray):void {
			var file:File = new File();
			var fs:FileStream = new FileStream();
			file.nativePath = GlobalObject.projectDir+"/textures/"+name;
			trace(file.nativePath);
			fs.open(file,FileMode.WRITE);
			fs.writeBytes(bytes);
			fs.close();
		}
		/**
		 * 清除图片中纯白的内容，目的是为了让切图边缘的纯白色变为透明
		 */
		public function clearWhite(sourceData:BitmapData):void {
			for(var i:int=0;i<sourceData.width;i++) {
				for(var j:int=0;j<sourceData.height;j++) {
					if(sourceData.getPixel32(i,j)==0xffffffff) {
						sourceData.setPixel32(i,j,0xffffffff-1);
					}
				}
			}
		}
	}
}